from django.db.models import Q
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializer import *
import datetime

# Create your views here.


class UserView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = UserModel.objects.filter(username=username).first()
        if not user:
            return Response({'msg': '账号/密码 错误', 'code': 400})
        elif user.password != password:
            return Response({'msg': '账号/密码 错误', 'code': 400})
        else:
            ser = UserSer(user).data
            return Response({'msg': '登录成功', 'user': ser, 'code': 200})

    def put(self, request):
        user_id = request.data.get('user_id')
        user = UserModel.objects.filter(id=user_id).first()
        if user.level == '0':
            if user.money >= 40:
                user.level = 1
                user.money -= 40
                user.save()
                ser = UserSer(user).data
                return Response({'msg': '升级成功', 'user': ser, 'code': 200})
            else:
                return Response({'msg': '余额不足, 升级失败', 'code': 400})
        else:
            return Response({'msg': '已是VIP用户', 'code': 400})


class StoryView(APIView):
    def get(self, request):
        story_id = request.GET.get('story_id')
        if story_id:
            set = StoryModel.objects.filter(id=story_id)
        else:
            set = StoryModel.objects.all()
        return Response(StorySer(set, many=True).data)


class RecordView(APIView):
    def post(self, request):
        user_id = request.data.get('user_id')
        story_id = request.data.get('story_id')
        record = RecordModel.objects.filter(user_id=user_id, story_id=story_id, money=None).first()
        if record:
            return Response({'msg': '正在阅读中', 'code': 400})
        RecordModel.objects.create(user_id=user_id, story_id=story_id)
        return Response({'msg': '开始计时', 'code': 200})

    def put(self, request):
        user_id = request.data.get('user_id')
        story_id = request.data.get('story_id')
        try:
            user_obj = UserModel.objects.filter(id=user_id).first()
            story_obj = StoryModel.objects.filter(id=story_id).first()
            num_obj = NumBer.objects.filter(user_id=user_id, story_id=story_id).first()
            record = RecordModel.objects.filter(user_id=user_id, story_id=story_id, money=None).first()
            if record:
                price = PriceModel.objects.filter(level=record.user.level).first().price
                end = datetime.datetime.now()
                time = round((end-record.start).total_seconds()/3600, 2)
                money = round(price * time, 2)
                record.end = end
                record.time = time
                record.money = money
                record.save()
                user_obj.money += money
                user_obj.save()
                story_obj.num += 1
                story_obj.save()
                if num_obj:
                    num_obj.num += 1
                    num_obj.save()
                else:
                    NumBer.objects.create(user_id=user_id, story_id=story_id, num=1)
                print(money)
            return Response({'msg': '阅读结束', 'code': 200})
        except Exception as e:
            print(e)
            return Response({'msg': '错误', 'code': 400})

    def get(self, request):
        set = StoryModel.objects.filter(Q(num__gt=3))
        record = RecordModel.objects.filter(story__in=set)
        ser = {
            'story_list': StorySer(set, many=True).data,
            'record_list': RecordSer(record, many=True).data
        }
        return Response(ser)


class NumBerView(APIView):
    def get(self, request):
        user_id = request.GET.get('user_id')
        set = NumBer.objects.filter(user_id=user_id)
        return Response(NumSer(set, many=True).data)